<?php
namespace Admin\Controller;
use Think\Controller;
/*
	活动相关关键字
*/
class RuleController extends InitController {
	
		/* 内容回复 */
	public function System(){
	
		if (IS_AJAX) {
			$settings = array(
				'default_period' => intval(I('post.default-period')),
			);
			//文字类型
			$postmodule = I('post.postmodule');
			if($postmodule == 'text'){
				
				$preg = array('/textvalue="(.*?)"/','/title="(.*?)"/');
				$d = I('post.default');
				$default = htmlspecialchars_decode($d);
			
				$default = ltrim($default,"<p>");
				//$default = rtrim($default,"</p>");
			
				$default = str_replace(array("<br />", "<br/>", "<br>","<p>"), "\r\n", $default);
				$default = str_replace('target="_self"', "", $default);
				$default = strip_tags($default,"<a>");
				$default = preg_replace($preg,'',$default);
					
				$w = I('post.welcome');
				$welcome = htmlspecialchars_decode($w);
				$welcome = ltrim($welcome,"<p>");
				//$welcome = rtrim($welcome,"</p>");
				$welcome = str_replace(array("<br />", "<br/>", "<br>","<p>"), "\r\n", $welcome);
				$welcome = str_replace('target="_self"', "", $welcome);
				$welcome = strip_tags($welcome,"<a>");
				$welcome = preg_replace($preg,'',$welcome);
				
				if(!empty($d)){
					$settings['default'] = $default;
					$array               = array('option_name'=>'default','option_value'=>$default);
				}
				if(!empty($w)){
					$settings['welcome'] = $welcome;
					$array               = array('option_name'=>'welcome','option_value'=>$welcome);
				}
			}
			
			
			$type = I('type','welcome');
			$account = D('Options')->getOpts(array('option_name'=>$type));
			
			if(!empty($account)){
				M('Options')->where(array('option_name'=>$type))->save($array);
			}else{
				M('Options')->add($array);
			}
			//M('Wechats')->where('weid = %d',$weid)->save($settings);
			$this->success(array('info'=>'更新成功！'));
		} 
		
		$op = I('get.op') ? I('get.op') :'welcome';
		
		
		//默认回复，关注回复
		$account_welcome = D('Options')->getOpts(array('option_name'=>'welcome'));
		$account_default = D('Options')->getOpts(array('option_name'=>'default'));
		//$account = M('Wechats')->where('weid = %d',$weid)->field('welcome,default')->find();
		$account['welcome'] = $account_welcome['welcome'];
		$account['default'] = $account_default['default'];
		
		$welcome = @unserialize($account['welcome']);
		if(is_array($welcome)){
			$account['welcome'] = $welcome;
		}

		$default = @unserialize($account['default']);
		if(is_array($default)){
			$account['default'] = $default;
		}
		
		if (is_array($account[$op])){
			$id = $account[$op]['id'];
			$wechatrow['rule'] = M('Rule')->where('id = %d',$id)->find();
			$wechatrow['keyword'] = M('RuleKeyword')->where('rid = %d',$id)->select();
			$wechatid = $account[$op]['id'];
			if($wechatrow['rule']['module'] =='news'){
				$wechatrow['news'] = M('NewsReply')->where('rid = %d',$wechatid)->field('id,title,description,thumb,url')->find();
			}
		}else{
			$text = str_replace(array("\r\n", "\r", "\n"), "<br />", $account[$op]);
		}
		
		//import('Vendor.Cache.cache');
		//$modules = cache_load('modules');
		
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		
		$this->assign(array(
			'op'=>$op,
			'wechatrow'=>$wechatrow,
			'text'=>$text,
			'modules'=>$modules,
			'type'=>$type,
			'topmenu'=>$topmenu,
			'sidebarmenu'=>$sidebarmenu
		));
		
		$this->display('System');
	}
	

	/* 新用户扫描回复 */
	public function ScanUpNew(){
	
		if (IS_AJAX) {
			$content = I('content','','trim');
			$default_content = I('default','','trim');
			
			$preg = array('/textvalue="(.*?)"/','/title="(.*?)"/');
			$d = $content;
			$default = htmlspecialchars_decode($d);
			
			
			$default = str_replace(array("\t","\r", "\n","\r\n"), array("","","",""), $default);
			$default = str_replace(array("<br />", "<br/>", "<br>","<p>","</p>"), array("\r\n","\r\n","\r\n","\r\n",""), $default);
			$default = str_replace('target="_self"', "", $default);
			$default = strip_tags($default,"<a>");
			$default = preg_replace($preg,'',$default);
			$default = ltrim($default,"\r\n");
			$content = rtrim($default,"\r\n");
			
			
			//判断长度是否超过500，超过长度报错
			$judge_length = check_content_length($content,500);
			if(!$judge_length){
				$this->ajaxReturn(array('status'=>0,'msg'=>'修改失败,文字长度超过了500字长度'));
			}
			
			$data = array(
				'content'=>$content,
				'default'=>$default_content,
				'last_change_time'=>NOW_TIME,
			);
			$rows_num = M('ReturnContent')->where(array('type'=>0))->save($data);
			if($rows_num > 0){
				$this->ajaxReturn(array('status'=>1,'msg'=>'保存成功'));
			}
		}
		$info = M('ReturnContent')->where(array('type'=>0))->field('content,default')->find();
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		$info['content'] = str_replace(array("\r\n", "\r", "\n"), array("<br />", "<br />", "<br />"), $info['content']);		
		$this->assign(array(
			'topmenu'=>$topmenu,
			'sidebarmenu'=>$sidebarmenu,
			'info'=>$info,
		));
		
		$this->display('ScanUpNew');
	}

	/* 老用户扫描回复 */
	/* public function ScanUpOld(){
	
		if (IS_AJAX) {
			$content = I('content','','trim');
			$default_content = I('default','','trim');
			
			
			$preg = array('/textvalue="(.*?)"/','/title="(.*?)"/');
			$d = $content;
			$default = htmlspecialchars_decode($d);
			//$default = ltrim($default,"<p>");
			//$default = rtrim($default,"</p>");
			$default = str_replace(array("<br />", "<br/>", "<br>","<p>"), array("\r\n","\r\n","\r\n","\r\n",""), $default);
			$default = str_replace('target="_self"', "", $default);
			$default = strip_tags($default,"<a>");
			$default = preg_replace($preg,'',$default);
			$content = ltrim($default,"\r\n");
			
			//判断长度是否超过500，超过长度报错
			$judge_length = check_content_length($content,500);
			if(!$judge_length){
				$this->ajaxReturn(array('status'=>0,'msg'=>'修改失败,文字长度超过了500字长度'));
			}
			
			$data = array(
				'content'=>$content,
				'default'=>$default_content,
				'last_change_time'=>NOW_TIME,
			);
			$rows_num = M('ReturnContent')->where(array('type'=>1))->save($data);
			if($rows_num > 0){
				$this->ajaxReturn(array('status'=>1,'msg'=>'保存成功'));
			}
		}
		$info = M('ReturnContent')->where(array('type'=>1))->field('content,default')->find();
		$info['content'] = str_replace(array("\r\n", "\r", "\n"), "<br />", $info['content']);		
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$this->assign(array(
			'topmenu'=>$topmenu,
			'sidebarmenu'=>$sidebarmenu,
			'info'=>$info,
		));
		
		$this->display('ScanUpOld');
	} */
	
	/**
	*
	* 设置关注时回复 ， 没有关键字时回复
	*/
	/* public function Setdefaultwelcome(){
		$rid = intval(I('id'));
		$type = I('type');
		$weid = session('weid');
		if(empty($rid)){
			$this->error('缺少参数');
		}
		
		$rule = M('Rule')->where('id = %d',$rid)->field('id,module')->find();
		if (empty($rule)) {
			$this->error('抱歉，要设置的规则不存在或是已经被删除！');
		}
		
		$value = serialize(array(
			'module' => $rule['module'],
			'id' => $rid, 	
		));
		
		if ($type == 'default') {
			$data = array(
				'default' => $value,
			);
		} elseif ($type == 'welcome') {
			$data = array(
				'welcome' => $value,
			);
		}
	
		if(!empty($type)){
			$save=M('Wechats')->where('weid = %d',$weid)->save($data);
		}
		if($save){
			$this->success('系统回复更新成功！',U('Rule/System'));
		}
		$this->error('操作失败了哦！~');
		
	} */
	
	
	
	
	/* public function RuleDisplay(){
		import('Vendor.Cache.cache');
		$cacheModules = cache_load('modules');
		$weid = session('weid');
		
		$module = I('getmodule');

		$module = empty($module) ? 'all' : $module;
		$cids = $parentcates = $list =  array();
		$types = array('', '等价', '包含', '正则表达式匹配', '直接接管');

		$params = array();
		$params['weid'] = $weid;
		$status = I('status');
		if ($status != 'all') {
			if(isset($status) and !empty($status)){
			   $params['status'] = intval($status);
			}
		}
		$accountModules = D('Modules')->account_module();
		$modules = array();
		
		foreach($accountModules as $k => $v) {
				$modules[$v['name']] = $cacheModules[$v['name']];
		}
		$keyword = I('keyword');
		if(isset($keyword)) {
			$params['name'] = array('like',"%".$keyword."%");
		}
		if($module != 'all') {
			$params['module'] = $module;
		}
		$count = M('Rule')->where($params)->count();
		
		$Page       = new \Think\Page($count,10);
		$pager       = $Page->show();
		
		$list = M('Rule')->where($params)->order('status DESC, displayorder DESC, id ASC')->limit($Page->firstRow.",".$Page->listRows)->select();
	
		
		if (!empty($list)) {
			foreach($list as &$item) {
				$item['keywords'] = M('RuleKeyword')->where('rid = %d',$item['id'])->order('displayorder DESC, id DESC')->select();
			}
		}

		$account = M('Wechats')->where('weid = %d',$weid)->field('default,welcome')->find();
		$temp = unserialize($account['default']);
		if (is_array($temp)) {
			$account['default'] = $temp;
			$account['defaultrid'] = $temp['id'];
		}
		$temp = unserialize($account['welcome']);
		if (is_array($temp)) {
			$account['welcome'] = $temp;
			$account['welcomerid'] = $temp['id'];
		}
		
		$this->assign(array(
			'cacheModules'=>$cacheModules,
			'account'=>$account,
			'modules'=>$modules,
			'list'=>$list,
			'pager'=>$pager,
		));
		
		$this->display('RuleDisplay');
	} */
	
	
}	